package com.etymgiko.spaceshipshop.dao.hibernate;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.etymgiko.spaceshipshop.dao.UserDao;
import com.etymgiko.spaceshipshop.model.User;

import java.util.List;

/**
 * Dao to work with User object implemented using Hibernate.
 *
 * @author Ivan Holub
 */
public class UserDaoHibernate  extends HibernateDaoSupport implements UserDao {
    /**
     * {@inheritDoc} 
     */
    public User getUser(Long userId) {
        return (User) getHibernateTemplate().get(User.class, userId);
    }

    /**
     * {@inheritDoc}
     */
    public User findUser(String login, String password) {
        List list = getHibernateTemplate().find("from User user where user.login = ? and user.password = ?",
                new Object[]{login, password});
        User retValue = null;
        if (list.size() > 0) {
            retValue = (User) list.get(0);
        }
        return retValue;
    }
}
